using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class RigidbodyProjectile:Projectile {

	new Rigidbody2D rigidbody;
	float initialSpeed;

	private void OnCollisionEnter2D(Collision2D collision) {
		string otherLayer = LayerMask.LayerToName(collision.gameObject.layer);
		ContactPoint2D contact = collision.GetContact(0);

		RaycastHit2D hit = new RaycastHit2D();
		hit.normal=contact.normal;
		hit.point=contact.point;

		switch(otherLayer) {

		case "Wall":
			HitWall(collision.collider,hit,true);
			break;

		case "Mob":
			Mob other = collision.gameObject.GetComponent<Mob>();
			if(CanHit(other)) HitMob(other,contact.normal,contact.point);
			break;

		}

	}

	protected override void Init() {
		base.Init();
		rigidbody=GetComponent<Rigidbody2D>();
		rigidbody.velocity=velocity;
		transform.rotation=new Angle(velocity).quaternion;
		initialSpeed=velocity.magnitude;
	}

	protected override void UpdateStats() {
		Quaternion rotation = transform.rotation;
		base.UpdateStats();
		transform.rotation=rotation;
	}

	protected override void UpdatePosition() { }
	protected override void UpdateHit() { }

	protected override DamageStats getDamageStat {
		get {
			DamageStats result = base.getDamageStat;
			result.amount.AddMultiply(rigidbody.velocity.magnitude/initialSpeed,float.MaxValue);
			return result;
		}
	}
}
